package cn.wzg.netbase;

import static java.net.HttpURLConnection.HTTP_OK;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import cn.wzg.netbase.http.HttpCallbackStringListener;
import cn.wzg.netbase.http.HttpUrlConnectionUtils;
import cn.wzg.netbase.utils.StreamUtil;

public class CollectActivity extends AppCompatActivity {
    private String link="https://www.wanandroid.com/lg/collect/list/0/json";
    private TextView tv_html;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            String json= (String) msg.obj;
            tv_html.setText(json);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_collect);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        tv_html=findViewById(R.id.tv_html);
        //------------------------原方法-----------------------------------------------
//        String session = getIntent().getStringExtra("session");
//        //新开线程进行网络访问
//        new Thread(){
//            @Override
//            public void run() {
//                try {
//                    URL url=new URL(link);//创建URL对象
//                    //获取HttpsURLConnection对象
//                    HttpsURLConnection conn= (HttpsURLConnection) url.openConnection();
//                    conn.setConnectTimeout(5000);//设置超时时间（毫秒）
//                    if(session!=null)
//                        conn.setRequestProperty("Cookie", session);
//                    //判断网络访问是否成功
//                    if(conn.getResponseCode()==HTTP_OK){
//                        //获取输入流
//                        InputStream stream = conn.getInputStream();
//                        //从流中获取字符串
//                        String html= StreamUtil.stream2String(stream);
//                        Message msg=new Message();
//                        msg.obj=html;
//                        handler.sendMessage(msg);
//                    }
//                    conn.disconnect();//关闭连接
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }.start();
        //------------------------------------------------------------------------------
        HttpUrlConnectionUtils.doGet(this, link, new HttpCallbackStringListener() {
            @Override
            public void onFinish(String response) {
                tv_html.setText(response);
            }

            @Override
            public void onError(Exception e) {

            }
        });
    }
}